'''
Author: xialang333@gmail.com
Date: 2025-08-04 14:33:44
LastEditTime: 2025-08-04 16:40:34
'''
from sqlalchemy.orm import Session
from app import models, schemas

def get_exporter(db: Session, exporter_id: int):
    return db.query(models.DBExporter).filter(models.DBExporter.id == exporter_id).first()

def get_exporter_by_name(db: Session, name: str):
    return db.query(models.DBExporter).filter(models.DBExporter.name == name).first()

def get_exporters(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.DBExporter).offset(skip).limit(limit).all()

def create_exporter(db: Session, exporter: schemas.ExporterCreate):
    db_exporter = models.DBExporter(**exporter.dict())
    db.add(db_exporter)
    db.commit()
    db.refresh(db_exporter)
    return db_exporter

def update_exporter(db: Session, exporter_id: int, exporter: schemas.ExporterCreate):
    db_exporter = get_exporter(db, exporter_id)
    if db_exporter is None:
        return None
    for key, value in exporter.dict().items():
        setattr(db_exporter, key, value)
    db.commit()
    db.refresh(db_exporter)
    return db_exporter

def delete_exporter(db: Session, exporter_id: int):
    db_exporter = get_exporter(db, exporter_id)
    if db_exporter is None:
        return None
    db.delete(db_exporter)
    db.commit()
    return db_exporter